package client.control;

import base.BaseValue;
import client.entity.Good;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * @Description 上坪网络接口封装
  * @author 孔明浩
  * @Date 2020/4/8
  */
public class GoodControl {
    GoodControl(){}
    public ResponseEntity addGood(String token, String goodName, double price, int category, int count, String description, List<File> pic){
        ConnectControl connectControl = new ConnectControl();
        Map <String, String> params = new HashMap<>();
        params.put("goodName", goodName);
        params.put("price", price + "");
        params.put("category", category + "");
        params.put("count", count + "");
        params.put("description", description);

        Map<String, File> files = new HashMap<>();
        for (int i = 0; i < pic.size(); i++) {
            files.put("pic" + i,pic.get(i));
        }
        connectControl.doPost(BaseValue.load + "addGood", token,params,files);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity modifyStatus(String token, int goodId){
        return goodBase(token, goodId, "modifyStatus");
    }

    public ResponseEntity getGoodList(){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "getGoodList", null, null);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity getGoodInfo(int goodId){
        return goodBase(null,goodId, "getGoodInfo");
    }

    public ResponseEntity setGoodInfo(String token,int goodId, String goodName, double price, int category, int count, String description, List<File> pic){
        ConnectControl connectControl = new ConnectControl();
        Map <String, String> params = new HashMap<>();
        params.put("goodId",goodId + "");
        params.put("goodName", goodName);
        params.put("price", price + "");
        params.put("category", category + "");
        params.put("count", count + "");
        params.put("description", description);

        Map<String, File> files = new HashMap<>();
        for (int i = 0; i < pic.size(); i++) {
            files.put("pic" + i,pic.get(i));
        }

        connectControl.doPost(BaseValue.load + "setGoodInfo", token,params,files);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity deleteGood(String token, int goodId){
        return goodBase(token, goodId, "deleteGood");
    }

    public ResponseEntity purchase(String token, int goodId, int count){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("goodId", goodId + "");
        map.put("count", count + "");
        connectControl.doPost(BaseValue.load + "purchase", token, map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity getGoodByKey(String key, int page, int num){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("key", key);
        map.put("page", page + "");
        map.put("num", num + "");
        connectControl.doPost(BaseValue.load + "getGoodByKey", null, map);
        return ResponseEntity.fit(connectControl);
    }

    /**
     * 添加购物车
     * @param token token
     * @param goodId 商品Id
     * @return 返回类
     */
    public ResponseEntity addCart(String token, int goodId){
        return goodBase(token, goodId, "addCart");
    }

    public ResponseEntity deleteCart(String token, int goodId){
        return goodBase(token, goodId, "deleteCart");
    }

    public ResponseEntity addGoodCollection(String token, int goodId){
        return goodBase(token, goodId, "addGoodCollection");
    }

    public ResponseEntity deleteGoodCollection(String token, int goodId){
        return goodBase(token, goodId, "deleteGoodCollection");
    }

    public List<ImageIcon> getGoodImage(int goodId){
        ResponseEntity responseEntity = getGoodInfo(goodId);
        List<ImageIcon> images = new ArrayList<>();
        if(responseEntity.code == 0){
            Good good = JSONObject.parseObject(responseEntity.data, Good.class);
            List<String> picsUrl = JSONObject.parseArray(good.getPic(), String.class);
            for (String s:picsUrl){
                try{
                    ImageIcon imageIcon = new ImageIcon(new URL(s));
                    images.add(imageIcon);
                } catch (MalformedURLException e){
                    e.printStackTrace();
                }
            }
        }
        return images;
    }

    private static ResponseEntity goodBase(String token, int goodId, String action){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("goodId", goodId + "");
        connectControl.doPost(BaseValue.load + action, token, map);
        return ResponseEntity.fit(connectControl);
    }
}
